package com.webproject.action.householdManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.householdManager.Complain;
import com.webproject.entity.householdManager.HouseApproval;
import com.webproject.entity.householdManager.Household;
import com.webproject.service.householdManager.HouseholdManagerService;

public class HouseholdManagerAction extends BaseAction {

	private Page mpage;
	private HouseholdManagerService householdManagerService;

	public Page getMpage() {
		return mpage;
	}

	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}

	public void setHouseholdManagerService(
			HouseholdManagerService householdManagerService) {
		this.householdManagerService = householdManagerService;
	}

	public String queryHousehold() {
		mpage = getPage();
		householdManagerService.queryHousehold(mpage);
		setTotal(mpage);
		return SUCCESS;
	}

	public String editHousehold() {
		Household household = getHousehold();
		if (getOper().equals("add")) {
			householdManagerService.saveHousehold(household);
		} else if (getOper().equals("edit")) {
			householdManagerService.updateHouseHold(household);
		} else if (getOper().equals("del")) {
		}
		return SUCCESS;
	}

	private Household getHousehold() {
		Household household = new Household();
		String id = getRequest().getParameter("id");
		String name = getRequest().getParameter("name");
		String sex = getRequest().getParameter("sex");
		String phone = getRequest().getParameter("phone");
		String number = getRequest().getParameter("number");
		String roomId = getRequest().getParameter("roomId");
		String acceptTime = getRequest().getParameter("acceptTime");
		String liveTime = getRequest().getParameter("liveTime");
		String hasLive = getRequest().getParameter("hasLive");
		String hasCertificate = getRequest().getParameter("hasCertificate");
		String hasBilling = getRequest().getParameter("hasBilling");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		household.setId(isNotEmpty(id) ? Integer.parseInt(id) : 0);
		household.setRoomId(roomId);
		try {
			if (isNotEmpty(acceptTime)) {
				household.setAcceptTime(sdf.parse(acceptTime));
			}
			if (isNotEmpty(liveTime)) {
				household.setLiveTime(sdf.parse(liveTime));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		household.setHasLive(Boolean.parseBoolean(hasLive));
		household.setHasBilling(Boolean.parseBoolean(hasBilling));
		household.setHasCertificate(Boolean.parseBoolean(hasCertificate));
		household.setName(name);
		household.setSex(sex);
		household.setPhone(phone);
		household.setNumber(isNotEmpty(number) ? Integer.parseInt(number) : 0);
		return household;
	}

	public String queryHouseApproval() {
		mpage = getPage();
		String holdId = getRequest().getParameter("holdId");
		mpage.setCondition(holdId);
		householdManagerService.queryHouseApproval(mpage);
		setTotal(mpage);
		return SUCCESS;
	}

	public String editHouseApproval() {
		HouseApproval approval = getHouseApproval();
		if (getOper().equals("add")) {
			householdManagerService.saveHouseApproval(approval);
		} else if (getOper().equals("edit")) {
			householdManagerService.updateHouseApproval(approval);
		} else if (getOper().equals("del")) {
			householdManagerService.deleteHouseApproval(approval);
		}
		return SUCCESS;
	}

	private HouseApproval getHouseApproval() {
		HouseApproval approval = new HouseApproval();
		String id = getRequest().getParameter("id");
		String holdId = getRequest().getParameter("holdId");
		String wall = getRequest().getParameter("wall");
		String oozingWater = getRequest().getParameter("oozingWater");
		String doorWindow = getRequest().getParameter("doorWindow");
		String electricity = getRequest().getParameter("electricity");
		String water = getRequest().getParameter("water");
		String area = getRequest().getParameter("area");
		String approvalDate = getRequest().getParameter("approvalDate");
		String remark = getRequest().getParameter("remark");
		if (isNotEmpty(id)) {
			approval.setId(Integer.parseInt(id));
		}
		if(isNotEmpty(holdId)){
			approval.setHoldId(Integer.parseInt(holdId));
		}
			approval.setWall(wall);
			approval.setOozingWater(oozingWater);
			approval.setDoorWindow(doorWindow);
			approval.setElectricity(electricity);
			approval.setWater(water);
			approval.setArea(area);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(isNotEmpty(approvalDate)){
				approval.setApprovalDate(sdf.parse(approvalDate));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		approval.setRemark(remark);
		return approval;
	}
	
	/**
	 * 投诉
	 */
	public String queryComplain(){
		mpage = getPage();
		householdManagerService.queryComplain(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	
	public String editComplain(){
		Complain complain=getComplainItem();
		if (getOper().equals("add")) {
			householdManagerService.saveComplain(complain);
		} else if (getOper().equals("edit")) {
			householdManagerService.updateComplain(complain);
		} else if (getOper().equals("del")) {
			householdManagerService.deleteComplain(complain);
		}
		return SUCCESS;
	}
	
	private Complain getComplainItem(){
		String id=getRequest().getParameter("id");
		String houseCode=getRequest().getParameter("houseCode");
		String people=getRequest().getParameter("people");
		String time=getRequest().getParameter("time");
		String content=getRequest().getParameter("content");
		String reContent=getRequest().getParameter("reContent");
		Complain c=new Complain();
		c.setId(id);
		c.setContent(content);
		c.setHouseCode(houseCode);
		c.setPeople(people);
		c.setReContent(reContent);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(isNotEmpty(time)){
				c.setTime(sdf.parse(time));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}
}
